package cn.edu.scau.cmi.linzhanyi.hibernate;


import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class Controller {
	@FXML
	private TextField addstudentname;
	@FXML
	private TextField alterstudentid;
	@FXML
	private TextField deletestudentid;
	@FXML
	private TextField alterstudentname;
	@FXML
	private TextField addteachername;
	@FXML
	private TextField alterteacherid;
	@FXML
	private TextField deleteteacherid;
	@FXML
	private TextField alterteachername;
	@FXML
	private TableView<Student> studenttable;
	@FXML
	private TableView<Teacher> teachertable;
	@FXML
	private TableColumn<Integer, Student> sid;
	@FXML
	private TableColumn<String, Student> sname;
	@FXML
	private TableColumn<Teacher, Student> stutor;
	@FXML
	private TableColumn<Integer, Teacher> tid;
	@FXML
	private TableColumn<String, Teacher> tname;
	@FXML
	private TextField SselectTtextfield;
	@FXML
	private ComboBox<Teacher> SselectTcombobox;
	@FXML
	private Button SselectTbutton;

	public void addstudent() {
		if (addstudentname.getText()!=null) {
			HibernateCRUD.createStudent(new Student(addstudentname.getText()));
			addstudentname.setText(null);
		}
	}
	public void alterstudent() {
		if (alterstudentname.getText()!=null&&alterstudentid.getText()!=null) {
			HibernateCRUD.updateStudent(Integer.valueOf(alterstudentid.getText()), alterstudentname.getText());
			alterstudentname.setText(null);
			alterstudentid.setText(null);
		}
	}
	public void deletestudent() {
		if (deletestudentid.getText()!=null) {
			HibernateCRUD.deleteStudent(Integer.valueOf(deletestudentid.getText()));
			deletestudentid.setText(null);
		}
	}
	public void addteacher() {
		if (addteachername.getText()!=null) {
			HibernateCRUD.createTeacher(new Teacher(addteachername.getText()));
			addteachername.setText(null);
		}
	}
	public void alterteacher() {
		if (alterteachername.getText()!=null&&alterteacherid.getText()!=null) {
			HibernateCRUD.updateTeacher(Integer.valueOf(alterteacherid.getText()), alterteachername.getText());
			alterteacherid.setText(null);
			alterteachername.setText(null);
		}
	}
	public void deleteteacher() {
		if (deleteteacherid.getText()!=null) {
			HibernateCRUD.deleteTeacher(Integer.valueOf(deleteteacherid.getText()));
			deleteteacherid.setText(null);
		}
	}
	public void showstudent() {
		ObservableList<Student> students = FXCollections.observableArrayList(HibernateCRUD.checkStudent());
		studenttable.setItems(students);		
		sid.setCellValueFactory(new PropertyValueFactory<Integer, Student>("id"));
		sname.setCellValueFactory(new PropertyValueFactory<String, Student>("name"));
		stutor.setCellValueFactory(new PropertyValueFactory<Teacher, Student>("tutor"));
			
	}
	public void showteacher() {
		ObservableList<Teacher> teachers = FXCollections.observableArrayList(HibernateCRUD.checkTeacher());
		teachertable.setItems(teachers);
		tid.setCellValueFactory(new PropertyValueFactory<Integer, Teacher>("id"));
		tname.setCellValueFactory(new PropertyValueFactory<String, Teacher>("name"));

	}
	public void showtutor() {
		ObservableList<Teacher> teachers = FXCollections.observableArrayList(HibernateCRUD.checkTeacher());
		SselectTcombobox.setItems(teachers);
	}
	public void SselectT() {
		HibernateCRUD.updateStudent(Integer.valueOf(SselectTtextfield.getText()),SselectTcombobox.getSelectionModel().getSelectedItem());
		SselectTtextfield.setText(null);
		SselectTcombobox.setItems(null);
	}
	
}
